#!/bin/bash
# Copyright VMware, Inc.
# SPDX-License-Identifier: APACHE-2.0
#
# Environment configuration for nacos

# The values for all environment variables will be set in the below order of precedence
# 1. Custom environment variables defined below after Bitnami defaults
# 2. Constants defined in this file (environment variables with no default), i.e. BITNAMI_ROOT_DIR
# 3. Environment variables overridden via external files using *_FILE variables (see below)
# 4. Environment variables set externally (i.e. current Bash context/Dockerfile/userdata)

# Load logging library
# shellcheck disable=SC1090,SC1091
. /opt/bitnami/scripts/liblog.sh

export BITNAMI_ROOT_DIR="/opt/bitnami"
export BITNAMI_VOLUME_DIR="/bitnami"

# Logging configuration
export MODULE="${MODULE:-nacos}"
export BITNAMI_DEBUG="${BITNAMI_DEBUG:-false}"

# By setting an environment variable matching *_FILE to a file path, the prefixed environment
# variable will be overridden with the value specified in that file
nacos_env_vars=(
    NACOS_HOME
    NACOS_CONF_DIR
    NACOS_LOGS_DIR
    NACOS_DAEMON_USER
    NACOS_DAEMON_GROUP
    NACOS_DEFAULT_HTTP_LISTEN_ADDRESS
    JAVA_HOME
    JAVA_OPTS
)
for env_var in "${nacos_env_vars[@]}"; do
    file_env_var="${env_var}_FILE"
    if [[ -n "${!file_env_var:-}" ]]; then
        if [[ -r "${!file_env_var:-}" ]]; then
            export "${env_var}=$(< "${!file_env_var}")"
            unset "${file_env_var}"
        else
            warn "Skipping export of '${env_var}'. '${!file_env_var:-}' is not readable."
        fi
    fi
done
unset nacos_env_vars

# Paths
export NACOS_HOME="${BITNAMI_ROOT_DIR}/nacos"
export NACOS_CONF_DIR="${NACOS_HOME}/conf"
export NACOS_LOGS_DIR="${BITNAMI_VOLUME_DIR}"

# System users (when running with a privileged user)
export NACOS_DAEMON_USER="nacos"
export NACOS_DAEMON_GROUP="nacos"

# Nacos configuration
export NACOS_DEFAULT_HTTP_LISTEN_ADDRESS="0.0.0.0" # only used at build time

# Java configuration
export JAVA_HOME="${JAVA_HOME:-${BITNAMI_ROOT_DIR}/java}"
export JAVA_OPTS="${JAVA_OPTS:-}"
